package com.dayuanit.dy16.atm.task;

import com.dayuanit.dy16.atm.entity.TransferOrder;
import com.dayuanit.dy16.atm.enums.TransferOrderStatusEnums;
import com.dayuanit.dy16.atm.mapper.TransferOrderMapper;
import com.dayuanit.dy16.atm.service.ITransferDelayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TransferInTask extends BaseTask{

    @Autowired
    private TransferOrderMapper transferOrderMapper;

    @Autowired
    private ITransferDelayService transferDelayService;

    @Scheduled(cron = "0/3 * * * * ?")
    @Override
    protected void doTask() {
        processTransferIn();
    }


    private void processTransferIn() {
        List<TransferOrder> transferOrders = transferOrderMapper.listOrders(TransferOrderStatusEnums.wait_transfer.getK());
        for (TransferOrder order : transferOrders) {
            try {
                transferDelayService.transferIn(order);
            } catch (Exception e) {
                e.printStackTrace();
                // 将转账订单改为待回滚
                transferOrderMapper.updateStatus(order.getId(), TransferOrderStatusEnums.rollback_transfer.getK(), order.getStatus());
            }

        }
    }


}
